<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="活动" prop="activityId">
        <el-select v-model="queryParams.activityId" placeholder="请选择活动" clearable size="small">
          <el-option
            v-for="item in activityList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="微信用户" prop="wxUserId">
        <el-input
          v-model="queryParams.wxUserId"
          placeholder="请输入微信用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="内容类型" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择内容类型" clearable size="small">
          <el-option
            v-for="dict in contentTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in statusOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ypp:dynamic:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ypp:dynamic:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dynamicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="60"/>
      <el-table-column label="活动名称" align="center" prop="activityId" :formatter="activityFormat"/>
      <el-table-column label="微信用户" align="center" prop="wxUserName" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="内容类型" align="center" prop="contentType" :formatter="contentTypeFormat" />
<!--      <el-table-column label="状态" align="center" prop="status" >-->
<!--        <template slot-scope="scope">-->
<!--          <span :class="'status_'+scope.row.status">{{statusFormat(scope.row)}}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ypp:dynamic:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ypp:dynamic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动动态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动" prop="activityId">
          <el-input v-model="form.activityId" placeholder="请输入活动" disabled=""/>
        </el-form-item>
        <el-form-item label="微信用户" prop="wxUserId">
          <el-input v-model="form.wxUserName" placeholder="请输入微信用户" disabled/>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
        <el-form-item label="内容类型">
          <el-select v-model="form.contentType" placeholder="请选择内容类型" disabled>
            <el-option
              v-for="dict in contentTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="视频" prop="videoPath" v-if="form.contentType == '1'">
          <video v-if="form.videoPath"  :src="base_url+form.videoPath" width="300px" controls></video>
        </el-form-item>
        <el-form-item label="图片" prop="imgPath" v-else>
          <div class="demo-image__preview" >
            <el-image v-for="item in form.imgs"
                      style="width: 100px; height: 100px"
                      :src="base_url + item"
                      :preview-src-list="previewList" >
            </el-image>
          </div>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button type="primary" @click="down" icon="el-icon-download">下载附件</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDynamic, getDynamic, delDynamic, addDynamic, updateDynamic, exportDynamic } from "@/api/ypp/dynamic";
import { listActivity } from "@/api/ypp/activity";

export default {
  name: "Dynamic",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 活动动态表格数据
      dynamicList: [],
      // 状态字典
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: undefined,
        wxUserId: undefined,
        content: undefined,
        contentType: undefined,
        status: undefined,
        videoPath: undefined,
        imgPath: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 内容类型字典
      contentTypeOptions: [],
      activityList: [],
      isCreated: 0,
      previewList:[],
      base_url: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    console.log('created')
    this.isCreated = 1;
    this.queryParams.activityId = this.$route.query.activityId;
    this.getList();
    this.getDicts("ypp_dynamic_content_type").then(response => {
      this.contentTypeOptions = response.data;
    });
    listActivity().then(response => {
      this.activityList = response.rows;
    });

  },
  activated(){
    console.log()
    if (this.isCreated == 0){
      console.log('activated')
      this.queryParams.activityId = this.$route.query.activityId;
      if (this.$route.params.to){
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = 10;
      }
      this.getList();
    }
    this.isCreated = 0;
  },
  methods: {
    /** 查询活动动态列表 */
    getList() {
      this.loading = true;
      listDynamic(this.queryParams).then(response => {
        this.dynamicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    activityFormat(row, column){
      var txt = '';
      this.activityList.filter(e =>{
        if (e.id == row.activityId){
          txt = e.name;
          return;
        }
      })
      return txt;
    },
    // 内容类型字典翻译
    contentTypeFormat(row, column) {
      return this.selectDictLabel(this.contentTypeOptions, row.contentType);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        activityId: undefined,
        wxUserId: undefined,
        content: undefined,
        contentType: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        status: "0",
        videoPath: undefined,
        imgPath: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动动态";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.previewList = [];

      const id = row.id || this.ids
      getDynamic(id).then(response => {
        this.form = response.data;
        if (response.data.imgs){
          response.data.imgs.filter(e=>{
            this.previewList.push(this.base_url + e);
          })
        }

        this.open = true;
        this.title = "查看活动动态";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateDynamic(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDynamic(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除活动动态编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDynamic(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有活动动态数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDynamic(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    //下载
    down(){
      window.location.href = this.base_url + "/common/download/yppDynamic/"+this.form.id;
    }
  }
};
</script>

<style>
  .status_0{
    color: gray!important;
  }
  .status_1{
    color: deepskyblue;
  }
  .status_2{
    color: green;
  }
  .status_3{
    color: red;
  }
  .status_4{
    color: red;
  }
</style>
